package com.zhou.common.mybatis.template.oss.mq.consumer.listener;

import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.order.ConsumeOrderContext;
import com.aliyun.openservices.ons.api.order.MessageOrderListener;
import com.aliyun.openservices.ons.api.order.OrderAction;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zhou.common.mybatis.template.oss.model.OssUploadResult;
import com.zhou.common.mybatis.template.oss.service.OssService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.StandardCharsets;

@Slf4j
@Component
public class OssKeysListener implements MessageOrderListener {

    @Autowired
    private ObjectMapper objectMapper;


    @Autowired
    private OssService ossService;


    @Override
    public OrderAction consume(Message message, ConsumeOrderContext context) {
        String body = new String(message.getBody(), StandardCharsets.UTF_8);
        log.info("receive body={}", body);
        try {
            OssUploadResult ossUploadResult = objectMapper.readValue(body, OssUploadResult.class);
            ossService.service(ossUploadResult);
        }catch (Exception e){
            return OrderAction.Success;
        }

        return OrderAction.Suspend;
    }


}
